<?php require_once('Connections/db_conn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_db_conn, $db_conn);
$query_Community_Partners = "SELECT ORGNAME FROM organization ORDER BY ORGNAME ASC";
$Community_Partners = mysql_query($query_Community_Partners, $db_conn) or die(mysql_error());
$row_Community_Partners = mysql_fetch_assoc($Community_Partners);
$totalRows_Community_Partners = mysql_num_rows($Community_Partners);

mysql_select_db($database_db_conn, $db_conn);
$query_Language = "SELECT LANG FROM `language` ORDER BY LANG ASC";
$Language = mysql_query($query_Language, $db_conn) or die(mysql_error());
$row_Language = mysql_fetch_assoc($Language);
$totalRows_Language = mysql_num_rows($Language);

mysql_select_db($database_db_conn, $db_conn);
$query_Workshop_Topic = "SELECT WRKSPTOPIC FROM workshoptopics ORDER BY WRKSPTOPIC ASC";
$Workshop_Topic = mysql_query($query_Workshop_Topic, $db_conn) or die(mysql_error());
$row_Workshop_Topic = mysql_fetch_assoc($Workshop_Topic);
$totalRows_Workshop_Topic = mysql_num_rows($Workshop_Topic);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
<?php
$layout = explode('&&&', file_get_contents('layout.html'));
echo $layout[0];

session_start();
if (!(isset($_SESSION['username'])))
	header("location:login.php");


$work_id=$_GET['id'];
mysql_select_db($database_db_conn, $db_conn);
$query_Workshop = "SELECT * FROM workshop WHERE WRKSPID = $work_id";
$Workshops= mysql_query($query_Workshop, $db_conn) or die(mysql_error());
$row_Workshop = mysql_fetch_assoc($Workshops);

$name=$row_Workshop['ORGID'];
$org_name = mysql_query("SELECT ORGNAME FROM organization WHERE ORGID=$name");
$workshop_org = mysql_fetch_assoc($org_name);

$get_time= getTime($row_Workshop['WRKSPTIME']);
$time_string = explode(" ", $get_time);
$time = $time_string[0];
$am_pm = $time_string[1];

$lang_id=$row_Workshop['WRKSPLANGID'];
$lang_name=mysql_query("SELECT LANG FROM LANGUAGE WHERE LANGID=$lang_id");
$workshop_lang=mysql_fetch_assoc($lang_name);

$topic_id=$row_Workshop['WRKSPTOPICID'];
$topic_name=mysql_query("SELECT WRKSPTOPIC FROM WORKSHOPTOPICS WHERE WRKSPTOPICID=$topic_id");
$workshop_topic=mysql_fetch_assoc($topic_name);

?>

<html>
<head>
 <script language="JavaScript" src="calendar3.js"></script>
</head>
<style type="text/css">
<!--
.style1 {color: #FF0000}
-->
</style>
<body>

<!-- Begin Main Column -->

<div id="mainContent">
<script language=javascript type='text/javascript'>
function showVisibility(obj)
{
	obj.style.visibility = 'visible';
}
function hideVisibility(obj)
{
	obj.style.visibility = 'hidden';
}
</script>	
	<h2>Edit Workshop</h2>
    <p></p>
    Note: Required fields denoted with an asterisk(*)
      
<form id="form1" method="post" action="">
	  <table width="412" border="0">
        <tr>
          <td width="161">* Community Partner:</td>
          <td width="241"><label>
          <select name="community_partner" id="community_partner">
            <?php
do {  
?><option value="<?php echo $row_Community_Partners['ORGNAME']?>"<?php if($row_Community_Partners['ORGNAME']==$workshop_org['ORGNAME']){echo "selected=\"selected\"";} ?>><?php echo $row_Community_Partners['ORGNAME']?></option>
            <?php
} while ($row_Community_Partners = mysql_fetch_assoc($Community_Partners));
  $rows = mysql_num_rows($Community_Partners);
  if($rows > 0) {
      mysql_data_seek($Community_Partners, 0);
	  $row_Community_Partners = mysql_fetch_assoc($Community_Partners);
  }
?>
          </select>
          </label></td>
        </tr>
        <tr>
          <td>* Date:</td>
          <td><label>
          <input name="date" type="text" id="date" size="10" maxlength="10" value="<?php echo $row_Workshop['WRKSPDATE']?>" />
          <a href="javascript:cal.popup();"><img src="img/cal.gif" /></a>
        </tr>
        <tr>
          <td>* Time:</td>
          <td><label>
          <input name="time" type="text" id="time" size="5" maxlength="5" value="<?php echo $time?>"/>
          <select name="am_pm" id="am_pm">
            <option value="a.m." <?php if($am_pm=="a.m.") {echo "selected=\"selected\"";}?>>a.m.</option>
            <option value="p.m." <?php if($am_pm=="p.m.") {echo "selected=\"selected\"";}?>>p.m.</option>
          </select>
          </label></td>
        </tr>
        <tr>
          <td>* Location:</td>
          <td><label>
          <input type="text" name="location" id="location" value="<?php echo $row_Workshop['LOCATION']?>"/>
          </td>
        </tr>
        <tr>
          <td>Address:</td>
          <td><label>
          <input type="text" name="address" id="address" value="<?php echo $row_Workshop['ADDRESS']?>"/>
          </label></td>
        </tr>
        <tr>
          <td>City:</td>
          <td><label>
          <input type="text" name="city" id="city" value="<?php echo $row_Workshop['CITY']?>"/>
          </label></td>
        </tr>
        <tr>
          <td>Zipcode:</td>
          <td><label>
          <input name="zipcode" type="text" id="zipcode" size="5" maxlength="5" value="<?php echo $row_Workshop['ZIPCODE']?>"/>
          </label></td>
        </tr>
        <tr>
          <td>* Language:</td>
          <td><label>
          <select name="language" id="language">
            <?php
do {
?><option value="<?php echo $row_Language['LANG']?>"<?php if ($row_Language['LANG'] == $workshop_lang['LANG']) {echo "selected=\"selected\"";} ?>onclick="hideVisibility(getElementById('L1'));getElementById('L1').value='';"><?php echo $row_Language['LANG']?></option>
            <?php
} while ($row_Language = mysql_fetch_assoc($Language));
  $rows = mysql_num_rows($Language);
  if($rows > 0) {
      mysql_data_seek($Language, 0);
	  $row_Language = mysql_fetch_assoc($Language);
  }
  
?>
		  <option value="Other" onclick="showVisibility(getElementById('L1'));">Other</option>
          </select>
          </label>
          <label><input name="other_Language" type="text" id='L1' size="25" maxlength="65" style="visibility:hidden;" /></label>
          </td>
        </tr>
        <tr>
          <td>Children Present?</td>
          <td>
              <input type="radio" name="children_present" value="Yes" id="yes_children_present" <?php if($row_Workshop['CHILDRENPRESENT']=="Yes") {echo "checked";}?>/>
              Yes<br />

              <input type="radio" name="children_present" value="Yes, childcare provided in another area" id="yes_care_provided" <?php if($row_Workshop['CHILDRENPRESENT']=="Yes, childcare provided in another area") {echo "checked";}?>/>
              Yes, childcare provided in another area<br />

              <input type="radio" name="children_present" value="No" id="no_children_present" <?php if($row_Workshop['CHILDRENPRESENT']=="No") {echo "checked";}?>/>
              No		  </td>
        </tr>
      </table>
    <p>Select Workshop Topic</p>
      <table width="411" height="61" border="0">
        <tr>
          <td width="162">* Workshop Topic:</td>
          <td width="239"><label>
                        <select name="workshop_topic_selection" id="workshop_topic_selection">
              <?php
do {
?><option value="<?php echo $row_Workshop_Topic['WRKSPTOPIC']?>"<?php if ($row_Workshop_Topic['WRKSPTOPIC'] == $workshop_topic['WRKSPTOPIC']) {echo "selected=\"selected\"";} ?>onclick="hideVisibility(getElementById('T1'));getElementById('T1').value='';"><?php echo $row_Workshop_Topic['WRKSPTOPIC']?> </option>
              <?php
} while ($row_Workshop_Topic = mysql_fetch_assoc($Workshop_Topic));
  $rows = mysql_num_rows($Workshop_Topic);
  if($rows > 0) {
      mysql_data_seek($Workshop_Topic, 0);
	  $row_Workshop_Topic = mysql_fetch_assoc($Workshop_Topic);
  }
?>
		  <option value="Other" onclick="showVisibility(getElementById('T1'));">Other</option>
          </select>
          </label>
          <label><input type="text" name="workshop_topic" id="T1" style="visibility:hidden;"/></label></td>
        </tr>
      </table>
	    <label>
	      <input type="submit" name="submit" id="submit" value="Submit" class= "button" />
        </label>
      </p>
      </form>
      <p>
      
      <script>var cal = new calendar3(document.forms['form1'].elements['date']);</script>
      
        <?php
	 	if($_POST) {
			ob_start();
			$host="localhost"; // Host name
			$username="root"; // Mysql username
			$password=""; // Mysql password
			$db_name="rtl"; // Database name
			$tbl_name="workshop"; // Table name
			
			// Connect to server and select databse.
			mysql_connect("$host", "$username", "$password")or die("cannot connect");
			mysql_select_db("$db_name")or die("cannot select DB");
			
			$community_partner=$_POST['community_partner'];
			$date=$_POST['date'];
			$time=$_POST['time'];
			$am_pm=$_POST['am_pm'];
			$location=$_POST['location'];
			$address=$_POST['address'];
			$city=$_POST['city'];
			$zipcode=$_POST['zipcode'];
			$language=$_POST['language'];
			$children_present=$_POST['children_present'];
			$workshop_topic_selection=$_POST['workshop_topic_selection'];
			$workshop_topic=$_POST['workshop_topic'];
			$workshop_id=$_GET['id'];
			$from_event=$_GET['event'];
			
			// To protect MySQL injection (more detail about MySQL injection)
			$date = stripslashes($date);
			$time = stripslashes($time);
			$location = stripslashes($location);
			$address = stripslashes($address);
			$city = stripslashes($city);
			$zipcode = stripslashes($zipcode);
			$workshop_topic = stripslashes($workshop_topic);

			$date = mysql_real_escape_string($date);
			$time = mysql_real_escape_string($time);
			$location = mysql_real_escape_string($location);
			$address = mysql_real_escape_string($address);
			$city = mysql_real_escape_string($city);
			$zipcode = mysql_real_escape_string($zipcode);
			$workshop_topic = mysql_real_escape_string($workshop_topic);
			
			//Process time
			$time = strtomySQLtime($time,$am_pm);
			
			$error=0;
			if($date != "" && $time != "" && $location != "" && ($workshop_topic != "" || $workshop_topic_selection != "None")) {
				if($workshop_topic != "") {
					$topic=$workshop_topic;
					$this_topic = mysql_query("SELECT * FROM workshoptopics WHERE WRKSPTOPIC='$topic'");
					$num_this_topic = mysql_num_rows($this_topic);
					if($num_this_topic==0) {
						mysql_query("INSERT INTO WORKSHOPTOPICS (WRKSPTOPIC) VALUES ('$topic')");
					}
					$get_workshop_topic_id = mysql_query("SELECT WRKSPTOPICID FROM WORKSHOPTOPICS WHERE WRKSPTOPIC='$topic'");
					$workshop_topic_id = mysql_result($get_workshop_topic_id,0);
				}
				else {
					$topic=$workshop_topic_selection;
					$get_workshop_topic_id = mysql_query("SELECT WRKSPTOPICID FROM WORKSHOPTOPICS WHERE WRKSPTOPIC='$topic'");
					$workshop_topic_id = mysql_result($get_workshop_topic_id,0);
				}
				
				if($other_language != "") {
					$lang=$other_language;
					$this_lang = mysql_query("SELECT * FROM language WHERE LANG='$lang'");
					$num_this_lang = mysql_num_rows($this_lang);
					if($num_this_lang==0) {
						mysql_query("INSERT INTO LANGUAGE (LANG) VALUES ('$lang')");
					}
					$get_workshop_lang_id = mysql_query("SELECT LANGID FROM LANGUAGE WHERE LANG='$lang'");
					$workshop_lang_id = mysql_result($get_workshop_lang_id,0);
				}
				else {
					$lang=$language;
					$get_workshop_lang_id = mysql_query("SELECT LANGID FROM LANGUAGE WHERE LANG='$lang'");
					$workshop_lang_id = mysql_result($get_workshop_lang_id,0);
				}
							
				$get_org_id = mysql_query("SELECT ORGID FROM ORGANIZATION WHERE ORGNAME='$community_partner'");
				$org_id = mysql_result($get_org_id,0);
		
				mysql_query("UPDATE $tbl_name SET WRKSPTOPICID='$workshop_topic_id', WRKSPLANGID='$workshop_lang_id', ORGID='$org_id', WRKSPDATE='$date', WRKSPTIME='$time', LOCATION='$location', ADDRESS='$address', CITY='$city', ZIPCODE='$zipcode',CHILDRENPRESENT='$children_present' WHERE WRKSPID=$workshop_id");
				
				echo "Workshop Successfuly Updated.";
			}
			else {
				echo "Required fields needed.";
				$error=1;
			}
			
			if(!$error && $from_event) {
				echo "<META HTTP-EQUIV='refresh' content='0;URL=EditEvent.php?id=$workshop_id>";
			}
					
			ob_end_flush();
		}
	?> 
</div>

<!-- Begin Side Column -->
<!-- Begin Footer -->

<?php
echo $layout[1];
?>

</body>
</html>

<?php
mysql_free_result($Community_Partners);

mysql_free_result($Language);

mysql_free_result($Workshop_Topic);
?>

<?php
function strtomySQLtime($timestring, $ampm_string)
{

	// split time into hour, minute, and second integers
	$time_array = explode(":", $timestring);
	
	$time_hour = $time_array[0];
	$time_minute = $time_array[1];
	$time_second = 00;
	
	// fix am/pm
	// if time is PM, add 12 to the hours integer
	if ($ampm_string == "p.m.")
	{
		$time_hour = $time_hour + 12;
	}
	if ($time_minute=="")
	{
		$time_minute=00;
	}
	$time= $time_hour.":".$time_minute.":".$time_second;
	return $time;
}

function getTime($timestring)
{

	// split time into hour, minute, and second integers
	$time_array = explode(":", $timestring);
	$time_hour = $time_array[0];
	$time_minute = $time_array[1];
	
	$am_pm = "a.m.";
	// fix am/pm
	// if time is PM, add 12 to the hours integer
	if ($time_hour > 12)
	{
		$time_hour = $time_hour-12;
		$am_pm = "p.m.";
	}
	$time= $time_hour.":".$time_minute." ".$am_pm;
	return $time;
}
?>